package com.dd.service.impl;

import com.dd.mapper.OrderItemMapper;
import com.dd.mapper.OrderMapper;
import com.dd.mapper.ProductMapper;
import com.dd.model.Order;
import com.dd.model.OrderItem;
import com.dd.model.Product;
import com.dd.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 订单服务实现类
 */
@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    private OrderMapper orderMapper;
    
    @Autowired
    private OrderItemMapper orderItemMapper;
    
    @Autowired
    private ProductMapper productMapper;
    
    @Override
    public List<Order> findAllOrders() {
        return orderMapper.findAll();
    }
    
    @Override
    public Order getOrderById(Integer orderId) {
        return orderMapper.findById(orderId);
    }
    
    @Override
    public List<Order> findOrdersByCustomer(Integer customerId) {
        return orderMapper.findByCustomerId(customerId);
    }
    
    @Override
    public List<Order> findOrdersByStatus(String status) {
        return orderMapper.findByStatus(status);
    }
    
    @Override
    @Transactional
    public boolean createOrder(Order order, List<OrderItem> orderItems) {
        // 设置订单日期
        if (order.getOrderDate() == null) {
            order.setOrderDate(new Date());
        }
        
        // 设置默认状态
        if (order.getStatus() == null || order.getStatus().isEmpty()) {
            order.setStatus("pending");
        }
        
        // 插入订单
        int result = orderMapper.insert(order);
        if (result <= 0) {
            return false;
        }
        
        // 插入订单项
        for (OrderItem item : orderItems) {
            // 获取商品信息
            Product product = productMapper.findById(item.getProductId());
            if (product == null) {
                continue;
            }
            
            // 设置订单项的订单ID
            item.setOrderId(order.getOrderId());
            
            // 设置单价
            if (item.getUnitPrice() == null) {
                item.setUnitPrice(product.getPrice());
            }
            
            // 插入订单项
            orderItemMapper.insert(item);
            
            // 更新商品库存
            int newStock = product.getStock() - item.getQuantity();
            if (newStock < 0) {
                newStock = 0;
            }
            productMapper.updateStock(product.getProductId(), newStock);
        }
        
        return true;
    }
    
    @Override
    @Transactional
    public boolean updateOrder(Order order) {
        return orderMapper.update(order) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateOrderStatus(Integer orderId, String status) {
        return orderMapper.updateStatus(orderId, status) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteOrder(Integer orderId) {
        // 先删除订单项
        orderItemMapper.deleteByOrderId(orderId);
        // 再删除订单
        return orderMapper.deleteById(orderId) > 0;
    }
    
    @Override
    public List<OrderItem> getOrderItems(Integer orderId) {
        return orderItemMapper.findByOrderId(orderId);
    }
    
    @Override
    @Transactional
    public boolean addOrderItem(OrderItem orderItem) {
        // 获取商品信息
        Product product = productMapper.findById(orderItem.getProductId());
        if (product == null) {
            return false;
        }
        
        // 设置单价
        if (orderItem.getUnitPrice() == null) {
            orderItem.setUnitPrice(product.getPrice());
        }
        
        // 插入订单项
        int result = orderItemMapper.insert(orderItem);
        
        // 更新商品库存
        int newStock = product.getStock() - orderItem.getQuantity();
        if (newStock < 0) {
            newStock = 0;
        }
        productMapper.updateStock(product.getProductId(), newStock);
        
        return result > 0;
    }
    
    @Override
    @Transactional
    public boolean updateOrderItem(OrderItem orderItem) {
        // 获取原订单项
        OrderItem oldItem = orderItemMapper.findById(orderItem.getItemId());
        if (oldItem == null) {
            return false;
        }
        
        // 获取商品信息
        Product product = productMapper.findById(orderItem.getProductId());
        if (product == null) {
            return false;
        }
        
        // 更新商品库存
        int stockChange = oldItem.getQuantity() - orderItem.getQuantity();
        int newStock = product.getStock() + stockChange;
        if (newStock < 0) {
            newStock = 0;
        }
        productMapper.updateStock(product.getProductId(), newStock);
        
        // 更新订单项
        return orderItemMapper.update(orderItem) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteOrderItem(Integer itemId) {
        // 获取订单项信息
        OrderItem item = orderItemMapper.findById(itemId);
        if (item == null) {
            return false;
        }
        
        // 归还商品库存
        Product product = productMapper.findById(item.getProductId());
        if (product != null) {
            int newStock = product.getStock() + item.getQuantity();
            productMapper.updateStock(product.getProductId(), newStock);
        }
        
        // 删除订单项
        return orderItemMapper.deleteById(itemId) > 0;
    }
} 